//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<io_embedded.cpp>
///	@path	~/src/python/core/embedded/
///	@date	2007/06/23
///	@desc	Embedded IO of python core.

#include "config/config.h"

#undef	getenv
#undef	stat
#undef	fopen

/*
#undef	fclose
#undef	fread
#undef	fwrite
#undef	fgetc
#undef	fputc
#undef	fgets
#undef	fputs
#undef	fgetpos
#undef	fsetpos
#undef	fseek
#undef	ftell
#undef	fseek64
#undef	ftell64
#undef	fprintf
#undef	vfprintf
#undef	feof
#undef	ferror
#undef	clearerr
#undef	fflush
#undef	rewind
#undef	setvbuf
#undef	tmpfile
*/

#include <sys/stat.h>

typedef struct _stat xstat;

#include <stdarg.h>

#include "database/store/IFileSystem.h"

#include "python/python_plug.h"

using namespace xeres;

// pure C linkage
extern "C" {

	// hack for get environment
	char * _hack_getenv( const char * name )
	{
		if( strcmp( name , "PYTHONCASEOK") == 0 )
			return "1";
		else
			return getenv( name );
	}

	// hack for stat() function
	int _hack_stat( const char * filename , void * buf )
	{
		WString path;
		DataInfo info;
		mbs_to_wcs( filename , path );

		//printf( "Stat: %s\n" , filename );

		if( !PythonPlug::instance().GetFilesystem()->FileExist( path ) &&
			!PythonPlug::instance().GetFilesystem()->DirectoryExist( path ) )
			return -1;

		PythonPlug::instance().GetFilesystem()->GetFileInfo( path , info );

		memset( buf , 0 , sizeof(xstat) );
		xstat * st = (xstat*)buf;
		st->st_ctime = (DateTimeStamp)info.m_created;
		st->st_atime = (DateTimeStamp)info.m_accessed;
		st->st_mtime = (DateTimeStamp)info.m_modified;
		switch( info.m_type )
		{
		case DATA_FILE:
			st->st_mode = _S_IFREG | _S_IREAD | _S_IWRITE;
			return 0;
		case DATA_DIR:
			st->st_mode = _S_IFDIR | _S_IREAD;
			return 0;
		default:
			return -1;
		}
	}

	// open file, check path
	FILE * _hack_fopen( const char * filename , const char * mode )
	{
		return PythonPlug::instance().GetFilesystem()->OpenPosixFile( filename , mode );
	}
/*
	// close file
	int _hack_fclose( FILE * file )
	{
		return fclose( file );
	}

	// read
	size_t _hack_fread( void * buffer , size_t size , size_t count , FILE * stream )
	{
		return fread( buffer , size , count , stream );
	}

	// write
	size_t _hack_fwrite( const void * buffer , size_t size , size_t count , FILE * stream )
	{
		return fwrite( buffer , size , count , stream );
	}

	// fgetc
	int _hack_fgetc( FILE * stream )
	{
		return fgetc( stream );
	}

	// fputc
	int _hack_fputc( int c , FILE * stream )
	{
		return fputc( c , stream );
	}

	// fgets
	char * _hack_fgets( char * str , int n , FILE * stream )
	{
		return fgets( str , n , stream );
	}

	// fputs
	int _hack_fputs( const char * str , FILE * stream )
	{
		return fputs( str , stream );
	}

	// fgetpos
	int _hack_fgetpos( FILE * stream , fpos_t * pos )
	{
		return fgetpos( stream , pos );
	}

	// fsetpos
	int _hack_fsetpos( FILE * stream , const fpos_t * pos )
	{
		return fsetpos( stream , pos );
	}

	// fseek
	int _hack_fseek( FILE * stream , long offset , int origin )
	{
		return fseek( stream , offset , origin );
	}

	// ftell
	long _hack_ftell( FILE * stream )
	{
		return ftell( stream );
	}

	// fseek64
	int _hack_fseek64( FILE * stream , long offset , int origin )
	{
		return _fseeki64( stream , offset , origin );
	}

	// ftell64
	__int64 _hack_ftell64( FILE * stream )
	{
		return _ftelli64( stream );
	}

	// fprintf
	int _hack_fprintf( FILE * stream , const char * format , ... )
	{
		va_list ap;
		va_start( ap , format );
		int result = vfprintf( stream , format , ap );
		va_end( ap );
		return result;
	}

	// vfprintf
	int _hack_vfprintf( FILE * stream , const char * format , va_list argptr )
	{
		return vfprintf( stream , format , argptr );
	}

	// feof
	int _hack_feof( FILE * stream )
	{
		return feof( stream );
	}

	// ferror
	int _hack_ferror( FILE * stream )
	{
		return ferror( stream );
	}

	// clearerr
	void _hack_clearerr( FILE * stream )
	{
		clearerr( stream );
	}
	
	// fflush
	int _hack_fflush( FILE * stream )
	{
		return fflush( stream );
	}

	// rewind
	void _hack_rewind( FILE * stream )
	{
		rewind( stream );
	}

	// setvbuf
	int _hack_setvbuf( FILE * stream , char * buffer , int mode , size_t size )
	{
		return setvbuf( stream , buffer , mode , size );
	}

	// tmpfile
	FILE * _hack_tmpfile( void )
	{
		return tmpfile();
	}
*/
};
